/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.saveonhand.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.BdOnhandQuantity;
import com.lightkits.common.db.entity.FndWarehouse;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.domain.saveonhand.models.OnhandParams;
import com.lightkits.wms.domain.saveonhand.service.CreateOnhandDomain;
import com.lightkits.wms.util.models.Constrants;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * SaveOnhandDomainImpl
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/2/20 19:01
 */
@Service
public class CreateOnhandDomainImpl implements CreateOnhandDomain {

    @Override
    public CheckResult<BdOnhandQuantity> createOnhandQuan(OnhandParams onhand) {
        CheckResult<BdOnhandQuantity> checkResult = new CheckResult<>();
        checkResult.setSuccessful(true);

        EntityWrapper<BdOnhandQuantity> entityWrapper = new EntityWrapper<>();
        if (!ObjectUtil.isNullOrEmpty(onhand.getLotNumber())) {
            entityWrapper.eq(BdOnhandQuantity.LOT_NUMBER, onhand.getLotNumber());
        } else {
            entityWrapper.isNull(BdOnhandQuantity.LOT_NUMBER);
        }
        if (!ObjectUtil.isEmpty(onhand.getOwnerId())) {
            entityWrapper.eq(BdOnhandQuantity.OWNER_ID, onhand.getOwnerId());
        } else {
            entityWrapper.isNull(BdOnhandQuantity.OWNER_ID);
        }
        if (!ObjectUtil.isEmpty(onhand.getWmAreaId())) {
            entityWrapper.eq(BdOnhandQuantity.WM_AREA_ID, onhand.getWmAreaId());
        } else {
            entityWrapper.isNull(BdOnhandQuantity.WM_AREA_ID);
        }
        BdOnhandQuantity onhandQuantity = new BdOnhandQuantity().selectOne(entityWrapper
                .eq(BdOnhandQuantity.ORGANIZATION_ID, onhand.getOrgId())
                .eq(BdOnhandQuantity.ITEM_ID, onhand.getItemId())
                .eq(BdOnhandQuantity.PRIMARY_UOM, onhand.getUom())
                .eq(BdOnhandQuantity.WAREHOUSE_ID, onhand.getWarehouseId())
                .eq(BdOnhandQuantity.OWNER_TYPE, onhand.getOwnerType()));
        //create onhandQuality
        if (!ObjectUtil.isEmpty(onhandQuantity)) {
            if (onhand.getChangeDirection().equals(Constrants.OUT)) {
                onhandQuantity.setQuantity(onhandQuantity.getQuantity().subtract(onhand.getQuantity()));
            }
            if (onhand.getChangeDirection().equals(Constrants.IN)) {
                onhandQuantity.setQuantity(onhandQuantity.getQuantity().add(onhand.getQuantity()));
            }
            onhandQuantity.setPrimaryQuantity(onhandQuantity.getQuantity());
            FndWarehouse warehouse = new FndWarehouse().selectById(onhandQuantity.getWarehouseId());
            if (!warehouse.getNegativeFlag()
                    && onhandQuantity.getQuantity().compareTo(BigDecimal.ZERO) < 0) {
                return CheckResult.fail("该执行数量大于库存现有量");

            }
            onhandQuantity.setTransFlag(false);
            checkResult.setData(onhandQuantity);
        } else {
            FndWarehouse warehouse = new FndWarehouse().selectById(onhand.getWarehouseId());
            if (onhand.getChangeDirection().equals(Constrants.OUT)) {
                if (!warehouse.getNegativeFlag()) {
                    return CheckResult.fail("该仓库禁止负库存");
                } else {
                    onhand.setQuantity(new BigDecimal(0).subtract(onhand.getQuantity()));
                }
            }
            if (ObjectUtil.isNullOrEmpty(onhand.getOwnerType())) {
                return CheckResult.fail("the Owner Type is null");
            }
            BdOnhandQuantity onhandQuantity1 = createOnhand(onhand);
            checkResult.setData(onhandQuantity1);
            return checkResult;
        }
        return checkResult;
    }

    /**
     * 创建现有量
     *
     * @param onhand 现有量参数
     * @return BdOnhandQuantity
     */
    private BdOnhandQuantity createOnhand(OnhandParams onhand) {
        BdOnhandQuantity bdOnhand = new BdOnhandQuantity();
        bdOnhand.setOrganizationId(onhand.getOrgId());
        bdOnhand.setItemId(onhand.getItemId());
        bdOnhand.setUom(onhand.getUom());
        bdOnhand.setPrimaryUom(onhand.getUom());
        bdOnhand.setQuantity(onhand.getQuantity());
        bdOnhand.setTransFlag(false);
        bdOnhand.setPrimaryQuantity(onhand.getQuantity());
        if (ObjectUtil.isNullOrEmpty(onhand.getLotNumber())) {
            bdOnhand.setLotNumber(null);
        } else {
            bdOnhand.setLotNumber(onhand.getLotNumber());
        }
        bdOnhand.setWarehouseId(onhand.getWarehouseId());
        bdOnhand.setWmAreaId(onhand.getWmAreaId());
        bdOnhand.setOwnerType(onhand.getOwnerType());
        bdOnhand.setOwnerId(onhand.getOwnerId());

        return bdOnhand;
    }

}
